package com.zzb.myshowme.view.fragment;


import android.app.Fragment;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.zzb.myshowme.R;
import com.zzb.myshowme.bean.ChangePasswordReq;
import com.zzb.myshowme.utils.CustomToast;
import com.zzb.myshowme.utils.HttpManager;
import com.zzb.myshowme.wiget.ProgressDialogView;

import rx.Subscriber;

/**
 * A simple {@link Fragment} subclass.
 */
public class SecondPasswordFragment extends Fragment implements View.OnClickListener {

    View view;
    ImageView back_imageV;
    EditText  password_editText, password_again_editText;
    Button reset_button;

    boolean bool_password;
    boolean bool_password_again;

    String password;
    String password_again;

    public SecondPasswordFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_second_password, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {

        ProgressDialogView.Initial(getActivity(), "重置密码中..");

        back_imageV = (ImageView)view.findViewById(R.id.back_imageV);
        password_editText = (EditText) view.findViewById(R.id.password_editText);
        password_again_editText = (EditText) view.findViewById(R.id.password_again_editText);
        reset_button = (Button) view.findViewById(R.id.reset_button);

        back_imageV.setOnClickListener(this);
        reset_button.setOnClickListener(this);

        password_editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    bool_password = true;
                } else {
                    bool_password = false;
                }

                if (bool_password && bool_password_again) {
                    reset_button.setEnabled(true);
                } else {
                    reset_button.setEnabled(false);
                }
            }
        });

        password_again_editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    bool_password_again = true;
                } else {
                    bool_password_again = false;
                }

                if (bool_password && bool_password_again) {
                    reset_button.setEnabled(true);
                } else {
                    reset_button.setEnabled(false);
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (view != null) {
            ViewGroup vg = (ViewGroup) view.getParent();
            vg.removeView(view);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_imageV:
                getActivity().finish();
                break;
            case R.id.reset_button:
                changePassword();
                break;
        }
    }

    private void changePassword(){
        password = password_editText.getText().toString();
        password_again = password_again_editText.getText().toString();
        if (!password.equals(password_again)) {
            Toast.makeText(getActivity(), "两次密码输入不一致", Toast.LENGTH_SHORT).show();
            return;
        }
        if (password.length() < 6) {
            Toast.makeText(getActivity(), "密码的长度为6~16之间", Toast.LENGTH_SHORT).show();
            return;
        }

        reset_button.setEnabled(false);
        ProgressDialogView.show();

        ChangePasswordReq changePasswordReq = new ChangePasswordReq();

//        HttpManager.getInstance().doHttpRequest(HttpManager.getInstance().getHttpService().changePassword(changePasswordReq), new Subscriber<String>() {
        HttpManager.getInstance().doHttpRequest(HttpManager.getInstance().getHttpService().testReq(), new Subscriber<String>() {
            @Override
            public void onCompleted() {
                Log.d("upload", "onCompleted down 成功======");

            }

            @Override
            public void onError(Throwable e) {
                Log.d("down", "onError 上传头像======" + e.getMessage());
                CustomToast.show(getActivity(), "网络连接失败，请查看网络设置", Toast.LENGTH_LONG);
                reset_button.setEnabled(true);
                ProgressDialogView.dismiss();
            }

            @Override
            public void onNext(String string) {
                reset_button.setEnabled(true);
                ProgressDialogView.dismiss();
                CustomToast.show(getActivity(), "重置成功", Toast.LENGTH_LONG);
                getActivity().finish();
            }
        });
    }

}
